package cn.bdqn.dao.impl;

import cn.bdqn.bean.User;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public User login(String uname, String pwd) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        User user=null;
        try{
            connection=getConn();
            String sql="select * from news_users where uname=? and upwd=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2,pwd);
            resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                user=new User();
                user.setUid(resultSet.getInt("uid"));
                user.setUname(resultSet.getString("uname"));
                user.setUpwd(resultSet.getString("upwd"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return user;
}

    @Override
    public int register(User user) {
        String sql="insert into news_users values(default,?,?)";
        Object[] objects={user.getUname(),user.getUpwd()};
        return  upd(sql,objects);

    }
}
